package badugaclient;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;



public class Gui extends JFrame implements IObservable
{
	private GuiCards cards;
	private GuiAuctions auctions;
	private PlayerInfo pInfo;
	private ArrayList<IObserver> observers;
	private boolean changed;
	
	Gui(PlayerInfo pInfo)
	{
		super("Badugi");
		this.pInfo = pInfo;
		observers = new ArrayList<IObserver>();
		changed = false;
		
		setSize(800,600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new GridLayout());
		cards = new GuiCards(this,pInfo);
		add(BorderLayout.WEST,cards);
		auctions = new GuiAuctions(this,pInfo);
		add(BorderLayout.EAST,auctions);
	}
	
	public void setHand()
	{
		cards.setHand0(this.pInfo.toString(this.pInfo.getCard(0)));
		cards.setHand1(this.pInfo.toString(this.pInfo.getCard(1)));
		cards.setHand2(this.pInfo.toString(this.pInfo.getCard(2)));
		cards.setHand3(this.pInfo.toString(this.pInfo.getCard(3)));
	}
	
	public void displayPlayerBetAndBankroll()
	{
		auctions.setCachText("BANKROLL: " + Gui.this.pInfo.getMyTokens());
		auctions.setMyBet("BET: " + Gui.this.pInfo.getMyBetInt());
		auctions.setMaxBet("MAX BET: " + this.pInfo.getCurrentTopBet());
	}
	
	public void setDealer(String whoIsDealer)
	{
		if(whoIsDealer.equals(pInfo.getMyID()))
		{
			auctions.setInfoLabelText("DEALER BUTTON");
		}
		else
		{
			
		}
	}
	
	public void setBigBlind(String bigBlindPlayer)
	{
		if(bigBlindPlayer.equals(this.pInfo.getMyID()))
		{
			auctions.setBlindLabelText("BIG BLIND");
			auctions.setMyBet("BET: " + Gui.this.pInfo.getBigBlind());
		}
	}
	
	public void setSmallBlind(String smallBlindPlayer)
	{
		if(smallBlindPlayer.equals(this.pInfo.getMyID()))
		{
			auctions.setBlindLabelText("SMALL BLIND");
			auctions.setMyBet("BET: " + Gui.this.pInfo.getSmallBlind());
		}
	}
	
	public void auctionsButtonEnable()
	{
		if(Gui.this.pInfo.getMyTokens() < Gui.this.pInfo.getCurrentTopBet())
		{
			auctions.setAllInEnable();
			auctions.setFoldEnable();
		}
		else
		{
			if(Gui.this.pInfo.getMyBetInt() == Gui.this.pInfo.getCurrentTopBet())
			{
				auctions.setCheckEnable();
			}
			if(this.pInfo.isBetted() == false)
			{
				auctions.setBetEnable();
			}
			if(this.pInfo.isBetted() == true)
			{
				auctions.setRaiseEnable();
			}
			if(this.pInfo.getMyBetInt() != this.pInfo.getCurrentTopBet())
			{
				auctions.setCallEnable();
			}
			
			auctions.setFoldEnable();
		}
	}

	@Override
	public void addObserver(IObserver observer) 
	{
		if(observer != null)
		{
			observers.add(observer);
		}
		
		
	}

	@Override
	public void removeObserver(IObserver observer) 
	{
		observers.remove(observer);
		
	}

	@Override
	public void notifyObservers() 
	{
		if(changed)
		{
			for(IObserver ob : observers)
			{
				ob.updateGUIMain();
			}
			changed = false;
		}
		
	}

	@Override
	public void setChanged() 
	{
		changed = true;
		
	}
	
	public void displayWinner()
	{
		if(this.pInfo.getHowManyWinners() == 1)
		{
			if(this.pInfo.getWinnerID().equals(this.pInfo.getMyID()))
			{
				
			}
		}
	}
	
	public void disableFold()
	{
		auctions.setFoldDisable();
	}
	
	public void disableCheck()
	{
		auctions.setCheckDisable();
	}
	
	public void disableAllIn()
	{
		auctions.setAllInDisable();
	}
	
	public void disableCall()
	{
		auctions.setCallDisable();
	}
	
	public void exchangeEnabled()
	{
		cards.setExchangeEnabled();
	}
	
	public void exchangeDisable()
	{
		cards.setExchangeDisable();
	}
	
	public void winner()
	{
		if(this.pInfo.getHowManyWinners() == 1)
		{
			if(this.pInfo.getWinnerID().equals(this.pInfo.getMyID()))
			{
				cards.setWinner("Winner is: You !");
			}
			else
			{
				cards.setWinner("Winner is: Player" + this.pInfo.getWinnerID());
			}
		}
	}
	
	public void disableAll()
	{
		auctions.setCheckDisable();
		auctions.setAllInDisable();
		auctions.setFoldDisable();
		auctions.setCallDisable();
		cards.setExchangeDisable();
		auctions.setBetDisable();
		auctions.setRaiseDisable();
	}
	
	public void disableBet()
	{
		auctions.setBetDisable();
	}
	
	public void disableRaise()
	{
		auctions.setRaiseDisable();
	}
	
	public void nextRound()
	{
		int reply = JOptionPane.showConfirmDialog(null, "Do you want fight again young Jedi ?", "Poker Game Badugi I think", JOptionPane.YES_NO_OPTION);
		
		if(reply == JOptionPane.YES_OPTION)
		{
			JOptionPane.showMessageDialog(null, "For other players pleas wait");
			this.pInfo.setContent("Yes");
			setChanged();
			notifyObservers();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Dark side in you I feel");
			pInfo.setContent("No");
			setChanged();
			notifyObservers();
		}
		
	}
	
	public void displayeAuctionButtons()
	{
		auctions.setCheckDisable();
		auctions.setAllInDisable();
		auctions.setFoldDisable();
		auctions.setCallDisable();
		auctions.setBetDisable();
		auctions.setRaiseDisable();
	}
	
}
